
capture log close
log using $log/AED_analysis_05_sensitivity_measure text replace

/* ------------------------------------------------------------------------------------ */
/* --- Sensitivity Analysis Table A.7 ------------------------------------------------- */
/* ------------------------------------------------------------------------------------ */ 


clear
clear matrix
clear mata
set maxvar 10000
set matsize 2000
set more off

set trace off
   
#d;
global xvars " vac_rate aloq   
				share_agriculture_avg share_industry_avg share_service_avg wanderungen_avg bip_avg 
				iza_alter_kat1 iza_alter_kat2 iza_alter_kat3 iza_alter_kat4 iza_sex iza_famstd
				i.iza_kind i.iza_abschluss_kat i.iza_ausb_kat
				iza_last_daily_income i.iza_beruf_kat
				iza_ende_last iza_vztz_last iza_zeit_la
				iza_sv_tminus1 iza_sv_tminus2 iza_sv_tminus3
				iza_lnwage_tminus1 iza_lnwage_tminus2 iza_lnwage_tminus3 
				i.year i.entry_m";
				#d cr
				
				
			
	
	
		use $data/data_est_border, clear
		local opt "pair_* [pw=1/fweight_reg], cluster(ba_year)"
	
	
	keep if eintritt<=200805 & eintritt>=200706
	

	#d;
		egen month_sv1_24 = rsum(sv_tplus_1 sv_tplus_2 sv_tplus_3 sv_tplus_4 sv_tplus_5 sv_tplus_6 sv_tplus_7 sv_tplus_8 sv_tplus_9 sv_tplus_10 
		sv_tplus_11 sv_tplus_12 sv_tplus_13 sv_tplus_14 sv_tplus_15 sv_tplus_16 sv_tplus_17 sv_tplus_18 sv_tplus_19 sv_tplus_20 sv_tplus_21 sv_tplus_22 sv_tplus_23 sv_tplus_24)
		;
	
		egen earnings1_24 = rsum(daily_income_tplus_1 daily_income_tplus_2 daily_income_tplus_3 daily_income_tplus_4 daily_income_tplus_5
		daily_income_tplus_6 daily_income_tplus_7 daily_income_tplus_8 daily_income_tplus_9 daily_income_tplus_10 daily_income_tplus_11
		daily_income_tplus_12 daily_income_tplus_13 daily_income_tplus_14 daily_income_tplus_15 daily_income_tplus_16 
		daily_income_tplus_17 daily_income_tplus_18 daily_income_tplus_19 daily_income_tplus_20
		daily_income_tplus_21 daily_income_tplus_22 daily_income_tplus_23 daily_income_tplus_24);
		replace earnings1_24 = earnings1_24*30;
		#d cr
	
	
	
	gen intensity = exp(log_intensity_alt)
	
	gen log_intensity_eink = log(intensity/hheink)
	
	gen bland=floor(ba/100)
	winsor2 intensity, cut(5 95) replace by(bland)
	
	
	
	gen intensity_high=0
	
	forvalues x=1/487 {
		qui sum log_intensity_alt if pair_`x'==1
		qui replace intensity_high=1 if log_intensity_alt>r(mean) & pair_`x'==1 
	}
	
	gen intensity_high25=0
	forvalues x=1/487 {
		qui sum log_intensity_alt if pair_`x'==1 
		qui replace intensity_high25=1 if log_intensity_alt>(r(mean)+0.25) & pair_`x'==1 
	}
	
	
	
	
	
	
	foreach var in $outc {
		qui reg `var' log_intensity_alt $xvars pair_* [pw=1/fweight_reg], cluster(ba_year)
		matrix b=e(b)'
		matrix n=e(N)
		matrix v=e(V)
		matrix se=vecdiag(cholesky(diag(vecdiag(v[1..1,1..1]))))'
		qui sum `var'
		matrix m = r(mean)	
		matrix d1_`var'=b[1..1,1..1],se
		 
		qui reg `var' log_intensity_eink $xvars pair_* [pw=1/fweight_reg], cluster(ba_year)
		matrix b=e(b)'
		matrix n=e(N)
		matrix v=e(V)
		matrix se=vecdiag(cholesky(diag(vecdiag(v[1..1,1..1]))))'
		qui sum `var'
		matrix m = r(mean)
		matrix d2_`var'=b[1..1,1..1],se
		
		qui reg `var' log_intensity_exits $xvars pair_* [pw=1/fweight_reg], cluster(ba_year)
		matrix b=e(b)'
		matrix n=e(N)
		matrix v=e(V)
		matrix se=vecdiag(cholesky(diag(vecdiag(v[1..1,1..1]))))'
		qui sum `var'
		matrix m = r(mean)
		matrix d3_`var'=b[1..1,1..1],se
		 
		 
		 
		qui reg `var' intensity $xvars pair_* [pw=1/fweight_reg], cluster(ba_year)
		matrix b=e(b)'
		matrix n=e(N)
		matrix v=e(V)
		matrix se=vecdiag(cholesky(diag(vecdiag(v[1..1,1..1]))))'
		qui sum `var'
		matrix m = r(mean)
		matrix d4_`var'=b[1..1,1..1],se
		
		qui reg `var' intensity_high $xvars pair_* [pw=1/fweight_reg], cluster(ba_year)
		matrix b=e(b)'
		matrix n=e(N)
		matrix v=e(V)
		matrix se=vecdiag(cholesky(diag(vecdiag(v[1..1,1..1]))))'
		qui sum `var'
		matrix m = r(mean)
		matrix d5_`var'=b[1..1,1..1],se
		
		qui reg `var' intensity_high25 $xvars pair_* [pw=1/fweight_reg], cluster(ba_year)
		matrix b=e(b)'
		matrix n=e(N)
		matrix v=e(V)
		matrix se=vecdiag(cholesky(diag(vecdiag(v[1..1,1..1]))))'
		qui sum `var'
		matrix m = r(mean)
		matrix d6_`var'=b[1..1,1..1],se
		
		matrix top_`var' = d1_`var',d2_`var',d3_`var'
		matrix bottom_`var' = d4_`var',d5_`var',d6_`var'
		
	}
	


matrix top = top_month_sv1_24\top_earnings1_24\top_umzugskosten_24
matrix bottom = bottom_month_sv1_24\bottom_earnings1_24\bottom_umzugskosten_24

matrix colnames top = "Baseline" "SE" "by household income" "SE" "by exits" "SE"
matrix colnames bottom = "Level" "SE" "Dummy" "SE" "Dummy 25%" "SE"

matrix rownames top = "Months employed" "Earnings" "Relocation assistance"
matrix rownames bottom = "Months employed" "Earnings" "Relocation assistance"

mal list top

mat list bottom



log close
